
function MainStep2_PickCandi
%
% 绘制近地点庞加莱图，点击鼠标选择轨道初值
%
% 作者：张晨
% 邮箱：chenzhang@csu.ac.cn
% 单位：中国科学院空间应用工程与技术中心，空间探索室
% 时间：2022年02月24日
%%%%%%%%%%%%%%%%%%%%%%%%

clc; clear; close all; clear path;
% restoredefaultpath;
warning off;

% 添加模块
parentFolder = fileparts(pwd);
addpath(genpath([parentFolder , '\Basic']));
addpath(genpath([parentFolder , '\Bcr4bp']));
addpath(genpath([parentFolder , '\Cr3bp']));

global aux candi_LEO2LLO
global h1 h2

% 载入参数
aux = Bcr4bp_Aux;

% 载入结果
load candi_LEO2LLO;

tau0Vec = candi_LEO2LLO(: , 1);
x0Mtx = candi_LEO2LLO(: , 2:5);
taufVec = candi_LEO2LLO(: , 6);
xfMtx = candi_LEO2LLO(: , 7:10);

h1 = figure1;
h2 = figure2;

% 点鼠标画图
figure(2);
scatter(x0Mtx(: , 1) , x0Mtx(: , 2) , 50 , (taufVec - tau0Vec) * aux.dim.EMRot_t / 86400 , 'filled');
hc = colorbar;
hc.Label.String = 'Values';
% clim([70, 120]);
% colormap jet;
xlabel('x/LU');
ylabel('y/LU');
title('Scatter Plot with Colorbar');

plot_o([0 , 0] , aux.dim.rMagLEO / aux.dim.EMRot_l , 'b--' , 2)

dcm_obj = datacursormode(h2);
set(dcm_obj , 'DisplayStyle' , 'datatip' , 'SnapToDataVertex' , 'off' , 'Enable' , 'on')
set(dcm_obj , 'UpdateFcn' , @myupdatefcn);

end


function output = myupdatefcn(empt , event_obj)

global aux candi_LEO2LLO
global h1 h2

output = [];

tau0Vec = candi_LEO2LLO(: , 1);
x0Mtx = candi_LEO2LLO(: , 2:5);
taufVec = candi_LEO2LLO(: , 6);
xfMtx = candi_LEO2LLO(: , 7:10);

% ----------------------- 提取坐标 --------------------------
clf(h1);

xTemp = event_obj.Position(1);
yTemp = event_obj.Position(2);

disVec = sqrt((xTemp - x0Mtx(: , 1)).^2 + (yTemp - x0Mtx(: , 2)).^2);
[X , index] = min(disVec);

fprintf('%d \n' , index)

% 反向积分
figure1;
options = odeset('RelTol' , 1e-12 , 'AbsTol' , 1e-12);
[seg_tt , seg_xx] = ode113(@Bcr4bp_EqmEMRot2D , [taufVec(index) , tau0Vec(index)] , xfMtx(index , :)' , options , aux);

% 画图
plotOrb2D(seg_xx , 'b' , 2);
Bcr4bp_ScnEMRot(taufVec(index) , aux);

% 保存
picked_LEO2LLO = candi_LEO2LLO(index , :);
save picked_LEO2LLO picked_LEO2LLO

% save([pwd , '\result\picked_LEO2LLO.mat'] , 'picked_LEO2LLO');

end
